CXX = g++
CXXFLAGS = -std=c++11 -Wall -Wextra -O2
TARGET = list_program
SOURCES = src/main.cpp
HEADERS = src/core/SeqList.h src/core/LinkedList.h src/core/DoublyLinkedList.h \
          src/core/Polynomial.h src/algorithms/ReverseList.h \
          src/algorithms/RightShift.h src/core/StaticList.h \
          src/algorithms/GetNeighbors.h src/algorithms/Josephus.h
DOCS = docs/README.md
EXECUTABLE = $(TARGET)

SUBMIT_DIR = /home/lx
SUBMIT_NAME = 3024244114_List
SUBMIT_FILES = $(SOURCES) $(HEADERS) Makefile $(DOCS) $(EXECUTABLE)
SUBMIT_PATH = $(SUBMIT_DIR)/$(SUBMIT_NAME).zip

all: $(TARGET)

$(TARGET): $(SOURCES) $(HEADERS)
	@echo "正在编译 List 程序..."
	$(CXX) $(CXXFLAGS) -o $(TARGET) $(SOURCES)
	@echo "编译完成！可执行文件: $(TARGET)"

run: $(TARGET)
	@echo "正在运行 List 程序..."
	@echo "=========================================="
	./$(TARGET)

debug: CXXFLAGS += -g -DDEBUG
debug: $(TARGET)
	@echo "List 调试版本编译完成！"

clean:
	@echo "正在清理 List 目录..."
	rm -f $(TARGET)
	rm -f *.o
	rm -f *~
	rm -f core
	rm -f *.zip
	@echo "清理完成！"

submit: $(TARGET)
	@echo "正在打包 List 作业文件..."
	@mkdir -p $(SUBMIT_DIR)
	@echo "=== 打包文件列表 ==="
	@for file in $(SUBMIT_FILES); do \
		if [ -f "$$file" ]; then \
			echo "✓ $$file"; \
		else \
			echo "✗ $$file (缺失)"; \
		fi \
	done
	@echo "==================="
	@if command -v zip >/dev/null 2>&1; then \
		echo "使用 zip 打包..."; \
		cd . && zip -r $(SUBMIT_PATH) $(SUBMIT_FILES); \
		if [ $$? -eq 0 ] && [ -f "$(SUBMIT_PATH)" ]; then \
			echo "打包完成！文件: $(SUBMIT_PATH)"; \
			echo "文件大小:" `du -h $(SUBMIT_PATH) | cut -f1`; \
			if command -v unzip >/dev/null 2>&1; then \
				echo "包含文件:" `unzip -l $(SUBMIT_PATH) | tail -1 | awk '{print $$2}'` "个文件"; \
			else \
				echo "包含文件: (使用 'unzip -l $(SUBMIT_PATH)' 查看详情)"; \
			fi \
		else \
			echo "打包失败！请检查权限或磁盘空间"; \
		fi \
	else \
		echo "错误: zip 工具未安装，请运行: sudo apt install zip"; \
		exit 1; \
	fi
	@echo "文件位置: $(SUBMIT_DIR)"

info:
	@echo "=== List 项目文件信息 ==="
	@echo "工作目录: data-structure-chapter-2"
	@echo "源文件: $(SOURCES)"
	@echo "头文件: $(HEADERS)"
	@echo "文档文件: $(DOCS)"
	@echo "目标文件: $(TARGET)"
	@echo "编译器: $(CXX)"
	@echo "编译选项: $(CXXFLAGS)"
	@echo "提交包: $(SUBMIT_PATH)"
	@echo "提交文件: $(SUBMIT_FILES)"

check:
	@echo "=== 检查 List 目录文件存在性 ==="
	@for file in $(SOURCES) $(HEADERS) $(DOCS); do \
		if [ -f "$$file" ]; then \
			echo "✓ $$file"; \
		else \
			echo "✗ $$file (缺失)"; \
		fi \
	done
	@if [ -f "Makefile" ]; then echo "✓ Makefile"; else echo "✗ Makefile (缺失)"; fi
	@if [ -f "$(TARGET)" ]; then echo "✓ $(TARGET)"; else echo "✗ $(TARGET) (缺失)"; fi

test: $(TARGET)
	@echo "执行快速测试..."
	@echo "输入测试数据时可以使用: 1 2 3 4 5"
	@echo "=========================================="
	@./$(TARGET) || true

help:
	@echo "=== List 目录 Makefile 使用说明 ==="
	@echo "make all     - 编译程序（默认）"
	@echo "make run     - 编译并运行程序"
	@echo "make test    - 编译并快速测试"
	@echo "make debug   - 编译调试版本"
	@echo "make clean   - 清理编译文件"
	@echo "make submit   - 打包源代码和可执行文件到 /home/lx/ 目录"
	@echo "make info    - 显示项目信息"
	@echo "make check   - 检查文件完整性"
	@echo "make help    - 显示此帮助信息"

.PHONY: all run debug clean submit info check test help
